<!DOCTYPE html>
<html lang="en">

<head>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" type="text/javascript"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ"
	 crossorigin="anonymous"></script>
	<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb"
	 crossorigin="anonymous">
	<script type="text/javascript">




		var getDate = function (y, q) {
			var edate = "";
			var sdate = "";
			if (q == 1) {
				sdate = y + "-01-01"
				edate = y + "-03-31"
			}
			if (q == 2) {
				sdate = y + "-04-01"
				edate = y + "-06-30"
			}
			if (q == 3) {
				sdate = y + "-07-01"
				edate = y + "-09-30"
			}
			if (q == 4) {
				sdate = y + "-10-01"
				edate = y + "-12-31"
			}
			return [sdate, edate];
		};

		var url = window.location.href;
		var arrayUrl = url.split('=');
		var ou = arrayUrl[3];
		var arrayUrl1 = arrayUrl[2].split('&');
		var pe = arrayUrl1[0];
		var year = pe.split('Q')[0];
		var quarter = pe.split('Q')[1];
		var prog_id = "VCuHIFtJJSv";
		var dates = getDate(year, quarter);
		var startdate = dates[0];
		var enddate = dates[1];
		var med1 = "Amlodipine-5 mg";
		var med2 = "Atenolol-50 mg";
		var med3 = "Bisoprolol Fumarate-5 mg";
		var med4 = "Captopril-25 mg";
		var med5 = "Captopril-50 mg";
		var med6 = "Enalapril-10mg";
		var med7 = "Enalapril-20mg";
		var med8 = "Enalapril-5 mg";
		var med9 = "Hydrochlorothiazide-25 mg";
		var med10 = "Hydrochlorothiazide-50 mg";
		var med11 = "Isosorbide dinitrate-10 mg tab";
		var med12 = "Losartan/Hydrocholothiazide-12.5 mg";
		var med13 = "Methyldopa-250 mg";
		var med14 = "Nifedipine-30mg";
		var med15 = "Ramipril tab-5 mg";
		var med16 = "Ramipril 10mg";
		var med17 = "Valsartan 80mg (Diovan)";
		var med18 = "Bisoprolol hemifumarate 10 mg";
		var med19 = "Losartan potassium 50 mg ";
		var med20 = "Captopril 50 mg/hct 25 mg ";
		var med21 = "Losartan potassium 50 mg";
		var med22 = "Captopril 50 mg/hct 25 mg";

		var totalevent, totalEndevent = 0, totalHTN = 0, totalAHTN = 0, activeatendevent;
		var medOne = 0, medTwo = 0, medThree = 0, medFlag = 0;
		var totalh = 0;
		var checkStatus = function (data) {
			data.enrollments.forEach(function (element, index) {
				var tei = element.trackedEntityInstance;
				var count = 0, count2 = 0, count22 = 0, count3 = 0, flag = 0;
				var medCounter = 0;
				var EventAttr = "";
				var sIndex = 0, totalF = 0;
				$.when(
					$.getJSON("../api/events.json?orgUnit=" + ou + "&program=" + prog_id + "&programStage=Kr60c8j7vMe&trackedEntityInstance=" + tei + "&skipPaging=true", {
						format: "json"
					}),
					$.getJSON("../api/events.json?orgUnit=" + ou + "&program=" + prog_id + "&trackedEntityInstance=" + tei + "&orderBy=eventDate:DESC&skipPaging=true&endDate="+ enddate, {
						format: "json"
					})

				).then(function (data1, data2) {

					if (data2[0].events.length == 0) {
						return;
					}
					if (data2[0].events[0].programStage == "Kr60c8j7vMe") {
						console.log("exit stage");
						count = 1;
						var indexx = 1;
					}
					else if (data2.httpStatusCode == 409 || data2.httpStatusCode == "409") {
						count = 0;
						var indexx = 0;
					}
					else {
						count = 1;
						var indexx = 0;
					}

					for (var b = 0; b < data2[0].events.length; b++) {
						//sIndex = b;
						var date = data2[0].events[b].eventDate;
						var first = date.split('T')[0];
						var expireDate = new Date(first);
						var enddateLastYear = new Date(enddate);
						enddateLastYear.setFullYear(enddateLastYear.getFullYear() - 1);
						if (new Date(startdate) >= expireDate && expireDate <= enddateLastYear) {
							var currentEventAttr = data2[0].events[b].dataValues;
							for (var j = 0; j < currentEventAttr.length; j++) {
								if ((currentEventAttr[j].dataElement == "Ft37n3yO81y" || currentEventAttr[j].dataElement == "D4Z6XYfNQR9") && (currentEventAttr[j].value == "Newly_diagnosed" || currentEventAttr[j].value == "Previously_diagnosed")) {
									totalF = 1;
								 }
							}
							
						}

						// reporting period filter starts here	
						var currentEventAttr = data2[0].events[b].dataValues;
						if (new Date(enddate) >= expireDate && expireDate >= new Date(startdate)) {
							

							// attributes loop starts here	
							for (var j = 0; j < currentEventAttr.length; j++) {

								if ((currentEventAttr[j].dataElement == "Ft37n3yO81y" || currentEventAttr[j].dataElement == "D4Z6XYfNQR9") && (currentEventAttr[j].value == "Newly_diagnosed" || currentEventAttr[j].value == "Previously_diagnosed")) {
									count2 = 1; // count for total HTN Patients
									//if HTN patient found --  medications count loop start	
								}
							}
							// attributes loop ends here
							if(count2 == 1){
								for (var h = 0; h < currentEventAttr.length; h++) {
										if (currentEventAttr[h].value == med1) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med2) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med3) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med4) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med5) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med6) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med7) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med8) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med9) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med10) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med11) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med12) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med13) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med14) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med15) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med16) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med17) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med18) {
											medFlag++
											medCounter++;
										}
										if (currentEventAttr[h].value == med19) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med20) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med21) {
											medFlag++;
											medCounter++;
										}
										if (currentEventAttr[h].value == med22) {
											medFlag++;
											medCounter++;
										}
									}
								if(medFlag == 0){
									for (var b = 0; b < data2[0].events.length; b++) {
										var date = data2[0].events[b].eventDate;
										var first = date.split('T')[0];
										var expireDate = new Date(first);
										var currentEventAttr = data2[0].events[b].dataValues;
			
										if (expireDate < new Date(startdate)) {
											for (var j = 0; j < currentEventAttr.length; j++) {
			
												if ((currentEventAttr[j].dataElement == "Ft37n3yO81y" || currentEventAttr[j].dataElement == "D4Z6XYfNQR9") && (currentEventAttr[j].value == "Newly_diagnosed" || currentEventAttr[j].value == "Previously_diagnosed")) {
													count2 = 1; // count for total HTN Patients
			
													//if HTN patient found --  medications count loop starts
													for (var h = 0; h < currentEventAttr.length; h++) {
														if (currentEventAttr[h].value == med1) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med2) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med3) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med4) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med5) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med6) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med7) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med8) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med9) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med10) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med11) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med12) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med13) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med14) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med15) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med16) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med17) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med18) {
															medFlag++
															medCounter++;
														}
														if (currentEventAttr[h].value == med19) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med20) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med21) {
															medFlag++;
															medCounter++;
														}
														if (currentEventAttr[h].value == med22) {
															medFlag++;
															medCounter++;
														}
													}
												}
											}
											if(medFlag != 0){
												flag = 1;
												break;
											}
										}
									}
								}
								else{
									break;
								}
							}

						}
						// reporting period filter ends here
					}


					if (flag == 0) {

						

					}

					
					if (totalF == 1 && count == 0) { totalh++; }
					if (count2 == 1) { totalHTN++; }
					if (medCounter == 1) { medOne++; }
					else if (medCounter == 2) { medTwo++; }
					else if (medCounter >= 3) { medThree++; }
					document.getElementById('totalAHTN').innerHTML = medFlag;
					document.getElementById('totalHTN').innerHTML = totalHTN;
					document.getElementById('totalHTNM1').innerHTML = medOne;
					document.getElementById('totalHTNM2').innerHTML = medTwo;
					document.getElementById('totalHTNM3').innerHTML = medThree;
					document.getElementById('col5').innerHTML = totalh;
					document.getElementById('p').innerHTML = index + " of " + (data.enrollments.length - 1) + " completed";
					if (data.enrollments.length - 1 == index) {
						document.getElementById('loader').style.display = "none";
					}
				});

			});

		};


		$(document).ready(function () {

			$.when(
				$.getJSON("../api/enrollments.json?ou=" + ou + "&ouMode=DESCENDANTS&program=" + prog_id + "&fields=trackedEntityInstance&skipPaging=true&programEndDate=" + enddate, {
					format: "json"
				})
			).then(function (data1) {
				checkStatus(data1);
				

			});
		});
	</script>
	<style>
		table {
			table-layout: fixed;
			word-wrap: break-word;
			text-align: center;
		}

		/* Absolute Center Spinner */

		.loading {
			position: fixed;
			z-index: 999;
			height: 2em;
			width: 2em;
			overflow: show;
			margin: auto;
			top: 0;
			left: 0;
			bottom: 0;
			right: 0;
		}

		/* Transparent Overlay */

		.loading:before {
			content: '';
			display: block;
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, 0.3);
		}

		/* :not(:required) hides these rules from IE9 and below */

		.loading:not(:required) {
			/* hide "loading..." text */
			font: 0/0 a;
			color: transparent;
			text-shadow: none;
			background-color: transparent;
			border: 0;
		}

		.loading:not(:required):after {
			content: '';
			display: block;
			font-size: 10px;
			width: 1em;
			height: 1em;
			margin-top: -0.5em;
			-webkit-animation: spinner 1500ms infinite linear;
			-moz-animation: spinner 1500ms infinite linear;
			-ms-animation: spinner 1500ms infinite linear;
			-o-animation: spinner 1500ms infinite linear;
			animation: spinner 1500ms infinite linear;
			border-radius: 0.5em;
			-webkit-box-shadow: rgba(0, 0, 0, 0.75) 1.5em 0 0 0, rgba(0, 0, 0, 0.75) 1.1em 1.1em 0 0, rgba(0, 0, 0, 0.75) 0 1.5em 0 0, rgba(0, 0, 0, 0.75) -1.1em 1.1em 0 0, rgba(0, 0, 0, 0.5) -1.5em 0 0 0, rgba(0, 0, 0, 0.5) -1.1em -1.1em 0 0, rgba(0, 0, 0, 0.75) 0 -1.5em 0 0, rgba(0, 0, 0, 0.75) 1.1em -1.1em 0 0;
			box-shadow: rgba(0, 0, 0, 0.75) 1.5em 0 0 0, rgba(0, 0, 0, 0.75) 1.1em 1.1em 0 0, rgba(0, 0, 0, 0.75) 0 1.5em 0 0, rgba(0, 0, 0, 0.75) -1.1em 1.1em 0 0, rgba(0, 0, 0, 0.75) -1.5em 0 0 0, rgba(0, 0, 0, 0.75) -1.1em -1.1em 0 0, rgba(0, 0, 0, 0.75) 0 -1.5em 0 0, rgba(0, 0, 0, 0.75) 1.1em -1.1em 0 0;
		}

		/* Animation */

		@-webkit-keyframes spinner {
			0% {
				-webkit-transform: rotate(0deg);
				-moz-transform: rotate(0deg);
				-ms-transform: rotate(0deg);
				-o-transform: rotate(0deg);
				transform: rotate(0deg);
			}
			100% {
				-webkit-transform: rotate(360deg);
				-moz-transform: rotate(360deg);
				-ms-transform: rotate(360deg);
				-o-transform: rotate(360deg);
				transform: rotate(360deg);
			}
		}

		@-moz-keyframes spinner {
			0% {
				-webkit-transform: rotate(0deg);
				-moz-transform: rotate(0deg);
				-ms-transform: rotate(0deg);
				-o-transform: rotate(0deg);
				transform: rotate(0deg);
			}
			100% {
				-webkit-transform: rotate(360deg);
				-moz-transform: rotate(360deg);
				-ms-transform: rotate(360deg);
				-o-transform: rotate(360deg);
				transform: rotate(360deg);
			}
		}

		@-o-keyframes spinner {
			0% {
				-webkit-transform: rotate(0deg);
				-moz-transform: rotate(0deg);
				-ms-transform: rotate(0deg);
				-o-transform: rotate(0deg);
				transform: rotate(0deg);
			}
			100% {
				-webkit-transform: rotate(360deg);
				-moz-transform: rotate(360deg);
				-ms-transform: rotate(360deg);
				-o-transform: rotate(360deg);
				transform: rotate(360deg);
			}
		}

		@keyframes spinner {
			0% {
				-webkit-transform: rotate(0deg);
				-moz-transform: rotate(0deg);
				-ms-transform: rotate(0deg);
				-o-transform: rotate(0deg);
				transform: rotate(0deg);
			}
			100% {
				-webkit-transform: rotate(360deg);
				-moz-transform: rotate(360deg);
				-ms-transform: rotate(360deg);
				-o-transform: rotate(360deg);
				transform: rotate(360deg);
			}
		}

		button {
			cursor: pointer;
		}
	</style>

	<body>
		<div class="loading" id="loader">Loading&#8230;</div>

		<br>
		<table class="table table-bordered table-hover reporttable">
			<thead>
				<tr>
					<td style="width:50%">Number of perscribed antihypertensive medications during the reporting period</td>
					<td style="width:50%" id="totalAHTN"></td>
				</tr>
				<tr>
					<td style="width:50%">Total number of hypertensive patients seen in the reporting period</td>
					<td style="width:50%" id="totalHTN"></td>
				</tr>
				<tr>
					<td style="width:50%">Total number of hypertensive patients taken 1 medicine</td>
					<td style="width:50%" id="totalHTNM1"></td>
				</tr>
				<tr>
					<td style="width:50%">Total number of hypertensive patients taken 2 medicines</td>
					<td style="width:50%" id="totalHTNM2"></td>
				</tr>
				<tr>
					<td style="width:50%">Total number of hypertensive patients taken more than 3 medicines</td>
					<td style="width:50%" id="totalHTNM3"></td>
				</tr>
				<tr>
					<td style="width:50%">Total number of hypertensive patients on therapy ≥12 months by the end of the reporting period </td>
					<td style="width:50%" id="col5"></td>
				</tr>
				<tr>
					<td style="width:50%">Number of patients with latest Systolic BP recorded ≤140 AND latest Diastolic BP  ≤90mmHg on therapy ≥12 months by the
						end of the reporting</td>
					<td style="width:50%" id="col6"></td>
				</tr>
				<tr><td></td></tr>
				<tr>
					<td style="width:50%">Progress</td>
					<td style="width:50%" id="p"></td>
				</tr>
			</thead>
		</table>
	</body>

</html>